package com.bw.zg3park2212.Staff

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.StaffAdpter
import com.bw.zg3park2212.databinding.ActivityStaffBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path="/app/StaffActivity")
class StaffActivity : BaseActivity<ActivityStaffBinding,StaffViewModel>() {
    lateinit var staffAdpter: StaffAdpter
    override fun intitView() {
        staffAdpter= StaffAdpter()
        binding.rv.layoutManager=LinearLayoutManager(this)
        binding.rv.adapter=staffAdpter
        viewmodel.getstaff()
        staffAdpter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/StaffXiangActivity")
                .withString("staid",staffAdpter.data[position].id.toString())
                .navigation()
        }
        ItemTouchHelper(object:ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
            ): Int {
                var left=ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
               viewmodel.getstaffdel(staffAdpter.data[position].id)
                staffAdpter.remove(position)
            }
        }).attachToRecyclerView(binding.rv)
    }

    override fun intitData() {
        viewmodel.staffSuccse.observe(this){
            staffAdpter.data.clear()
            staffAdpter.addData(it)
            staffAdpter.notifyDataSetChanged()
        }
        viewmodel.staffDelSuccse.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

}